<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-server__serverInfo">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
		<el-form-item>
		<el-input size="mini" v-model="dataForm.keyContent" :placeholder="$t('keyContent')" clearable></el-input>
		</el-form-item>
		<el-form-item>
		 <el-select size="mini" v-model="dataForm.status" clearable  :placeholder="$t('status')" >
			 <el-option :key="1":label="$t('status1')":value="1"> </el-option>
		  <el-option :key="0":label="$t('status0')":value="0"> </el-option>
		   </el-select>
		</el-form-item>
        <el-form-item>
          <el-button size="mini" @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" v-if="$hasPermission('serverInfo:add')" type="primary" @click="addOrUpdateMethod(false,null)">{{ $t('add') }}</el-button>
        </el-form-item>
		<el-form-item>
		  <el-button size="mini" v-if="$hasPermission('serverInfo:del')" type="danger" @click="delHandle()">{{ $t('deleteBatch') }}</el-button>
		</el-form-item>
      </el-form>
	  
	  
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        @sort-change="dataListSortChangeHandle"
        style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
		<el-table-column prop="usePurpose" :label="$t('serverInfo.usePurpose')" header-align="center" align="center"></el-table-column>
        <el-table-column prop="serverHost" :label="$t('serverInfo.serverHost')"  header-align="center" align="center"></el-table-column>
		<el-table-column prop="serverPort" :label="$t('serverInfo.serverPort')" header-align="center" align="center"></el-table-column>
		<el-table-column prop="serverOsInf" :label="$t('serverInfo.serverOsInf')" header-align="center" align="center"></el-table-column>
		<el-table-column prop="cloudProvider" :label="$t('serverInfo.cloudProvider')"  header-align="center" align="center"></el-table-column>
		<el-table-column prop="remark" :label="$t('remark')" header-align="center" align="center"></el-table-column>
		
		<el-table-column v-if="!$hasPermission('serverInfo:modifyStatus')" prop="status" :label="$t('status')"  min-width="30px" header-align="center" align="center">
		  <template slot-scope="scope">
		    <el-tag type="danger" v-if="scope.row.status === 0">{{ $t('status0') }}</el-tag>
		    <el-tag v-if="scope.row.status === 1">{{ $t('status1') }}</el-tag>
		  </template>
		</el-table-column>
		
        <el-table-column v-if="$hasPermission('serverInfo:modifyStatus')"  prop="status" :label="$t('status')"  min-width="40px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch
              @change="changeStatus(scope.row)"
              v-model="scope.row.status === 1"
              size="mini"
              active-color="#7FAAF9"
              inactive-color="#999999"
            ></el-switch>
          </template>
        </el-table-column>
		<el-table-column prop="createTime" :label="$t('createTime')"  header-align="center" align="center" width="180"></el-table-column>
        <el-table-column prop="updateTime" :label="$t('updateTime')"  header-align="center" align="center" width="180"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="180">
          <template slot-scope="scope">
			<el-button v-if="$hasPermission('serverInfo:simpleGet')" type="text" size="small" @click="addOrUpdateMethod(true,scope.row.id)">{{ $t('detail') }}</el-button>
            <el-button v-if="$hasPermission('serverInfo:modify')" type="text" size="small" @click="addOrUpdateMethod(false,scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('serverInfo:del') && scope.row.id !== 1" type="text" size="small" @click="delHandle(scope.row.id)">{{ $t('delete') }}</el-button>
			<el-button v-if="$hasPermission('serverInfo:check')" type="text" size="small" @click="check(scope.row.id)">{{ $t('serverInfo.check') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageNo"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update :extendsParam="isOnlyRead" v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
	  
	  <!-- 测试连接弹框 -->
	  <el-dialog
	    title="返回内容"
	    :visible.sync="isShowResult"
	    width="30%"
	    >
	    <el-card class="box-card" 
			v-loading="checkCode" 
			element-loading-text="测试连接中..."
			    element-loading-spinner="el-icon-loading"
			>
	      <div v-for="item in resultList" :key="item" class="text item">
	        {{ item }}
	      </div>
	    </el-card>
	  </el-dialog>
	  
	  
    </div>
  </el-card>
  
  
  
  
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './serverInfo-addOrUpdate'
import { setMd5 } from '@/utils'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/pc/serverInfo/page',
        getDataListIsPage: true,
        deleteURL: '/pc/serverInfo/del',
        deleteIsBatch: true
      },
	  checkCode:false,
	  isShowResult: false,
	  isOnlyRead: false,
	  resultList: [],
      dataForm: {
        keyContent: '',
        status: ''
      }
    }
  },
  mounted() {

  },
  components: {
    AddOrUpdate
  },
  methods:{
	  changeStatus(obj){
		  let params = {
		      id: obj.id,
		      status: obj.status === 1 ? 0 : 1,
		  }
		  this.$http.post(`/pc/serverInfo/modifyStatus`,params).then(({ data: res }) => {
		      if(res.code === 200){
					obj.status = params.status;
		  			this.$message({type: 'success', message: '修改成功'})
		      }else{
				  this.$message.error(res.msg);
			  }
		  })
	  },
	  check(obj){
		  this.checkCode = true;
		  this.isShowResult = true;
		  this.resultList = [];
		  this.$http.get(`/pc/serverInfo/check`,{params:{id:obj}}).then(({ data: res }) => {
		      if(res.code === 200){
				   this.resultList = res.data
				   this.checkCode = false;
		      }else{
				  this.$message.error(res.msg);
			  }
		  })
	  },
	  addOrUpdateMethod(onlyType,thisid){
		  this.isOnlyRead = onlyType
		  this.addOrUpdateHandle(thisid)
	  }
  }
}
</script>

<style>
	.box-card .text {
		 font-size: 14px;
	}
	.box-card   .item {
		padding: 6px 0;
	}
</style>
